package org.openkoala.koala.wizard.wizardpage.component;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.openkoala.koala.widget.BusinessLog;
import org.openkoala.koala.widget.GeneralQuery;
import org.openkoala.koala.widget.Module;
import org.openkoala.koala.widget.Organization;
import org.openkoala.koala.widget.Project;
import org.openkoala.koala.widget.Security;

public class AdvancedSetupDialog extends Dialog {

	protected Object result;
	protected Shell shell;

	private ModuleManage moduleManage;
	
	private Project project;

	private enum SubSysType {
		SECURITY, ORGANIZATION, LOG, GENERAL_QUERY
	}
	
	public AdvancedSetupDialog(Project project, Shell shell) {
		super(shell);
		this.project = project;
	}

	@Override
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setText("高级设置");
	}
	
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite composite = new Composite(parent, SWT.NONE);
		initializeDialogUnits(composite);

		GridData mainGridData = new GridData(500, 450);
		composite.setLayoutData(mainGridData);

		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 1;
		composite.setLayout(gridLayout);
		
		createView(composite);
		return parent;
	}

	private void createView(Composite composite) {
		Module webModule = getWebModule();
		if (webModule.getSecurity() != null || webModule.getOrganization() != null
				|| webModule.getBusinessLog() != null || webModule.getGeneralQuery() != null) {
			generateAssemSourceCodeOptions(composite);
		}
		
		if (webModule.getSecurity() != null && webModule.getOrganization() != null) {
			generateAssemSecurityAndOrganizationOption(composite);
		}
		
		generateTechOptions(composite);
		
		moduleManage = new ModuleManage(project);
		moduleManage.getControl(composite);
		
		generateLocationOption(composite);
	}

	private void generateAssemSourceCodeOptions(Composite parent) {
		GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
		gridData.grabExcessHorizontalSpace = true;
		
		Group sourceCodeCheckListGroup = new Group(parent, SWT.NULL);
		sourceCodeCheckListGroup.setText("是否集成源代码");
		sourceCodeCheckListGroup.setLayout(new GridLayout(4, false));
		sourceCodeCheckListGroup.setLayoutData(gridData);
		
		Module module = getWebModule();
		if (module != null) {
			if (module.getSecurity() != null) {
				generateSourceCodeCheckButtons(module.getSecurity(), SubSysType.SECURITY, sourceCodeCheckListGroup);
			}
			if (module.getOrganization() != null) {
				generateSourceCodeCheckButtons(module.getOrganization(), SubSysType.ORGANIZATION, sourceCodeCheckListGroup);
			}
			if (module.getBusinessLog() != null) {
				generateSourceCodeCheckButtons(module.getBusinessLog(), SubSysType.LOG, sourceCodeCheckListGroup);
			}
			if (module.getGeneralQuery() != null) {
				generateSourceCodeCheckButtons(module.getGeneralQuery(), SubSysType.GENERAL_QUERY, sourceCodeCheckListGroup);
			}
		}
		
	}
	
	private Module getWebModule() {
		for (Module module : project.getModule()) {
			if (module.getModuleType().equals("war")) {
				return module;
			}
		}
		return null;
	}
	
	private void generateSourceCodeCheckButtons(final Object subSystem, final SubSysType subSysType, final Group group) {
		String description = "";
		if (subSysType == SubSysType.SECURITY) {
			description = "通用查询子系统";
		} else if (subSysType == SubSysType.ORGANIZATION) {
			description = "组织子系统";
		} else if (subSysType == SubSysType.LOG) {
			description = "业务日志子系统";
		} else if (subSysType == SubSysType.GENERAL_QUERY) {
			description = "通用查询子系统";
		}
		
		final Button checkButton = new Button(group, SWT.CHECK);
		checkButton.setText(description);
		checkButton.setSelection(true);
		checkButton.addSelectionListener(new SelectionListener() {
			
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (checkButton.getSelection()) {
					setAssemSourceCode(true);
				} else {
					setAssemSourceCode(false);
				}
			}
			
			private void setAssemSourceCode(boolean assemSourceCode) {
				if (subSysType == SubSysType.SECURITY) {
					Security security = (Security) subSystem;
					//TODO
				} else if (subSysType == SubSysType.ORGANIZATION) {
					Organization organization = (Organization) subSystem;
					//TODO
				} else if (subSysType == SubSysType.LOG) {
					BusinessLog businessLog = (BusinessLog) subSystem;
					//TODO
				} else if (subSysType == SubSysType.GENERAL_QUERY) {
					GeneralQuery generalQuery = (GeneralQuery) subSystem;
					//TODO
				}
			}
			
			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
			}
		});
	}
	
	private void generateAssemSecurityAndOrganizationOption(Composite parent) {
		GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
		gridData.grabExcessHorizontalSpace = true;
		
		Group assemSeAndOrgGroup = new Group(parent, SWT.NULL);
		assemSeAndOrgGroup.setText("关联选择");
		assemSeAndOrgGroup.setLayout(new GridLayout(1, false));
		assemSeAndOrgGroup.setLayoutData(gridData);
		
		final Button checkButton = new Button(assemSeAndOrgGroup, SWT.CHECK);
		checkButton.setText("是否让权限系统与组织系统相关联");
		checkButton.setSelection(true);
		checkButton.addSelectionListener(new SelectionListener() {
			
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (checkButton.getSelection()) {
					//TODO
				} else {
					//TODO
				}
			}
			
			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
			}
		});

	}
	
	private void generateTechOptions(Composite parent) {
		Group techListGroup = new Group(parent, SWT.NULL);
		techListGroup.setText("技术选项");
		techListGroup.setLayout(new GridLayout(5, false));
		techListGroup.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, true, false));
		
		Button demoButton = new Button(techListGroup, SWT.CHECK);
		demoButton.setText("生成示例代码");
		demoButton.addSelectionListener(new SelectionAdapter(){
			@Override
			public void widgetSelected(SelectionEvent event) {
				//TODO
			}
		});

		Label repositoryLabel = new Label(techListGroup, SWT.NONE);
		repositoryLabel.setText("ORM技术");
		final Combo repositoryCombo = new Combo(techListGroup, SWT.SINGLE | SWT.READ_ONLY);
		repositoryCombo.add("JPA");
		repositoryCombo.add("Mybatis");
		repositoryCombo.select(0);
		repositoryCombo.addSelectionListener(new SelectionAdapter(){
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				project.setDbProtocol(repositoryCombo.getText());
			}
		});
		
		Label mvcLabel = new Label(techListGroup, SWT.NONE);
		mvcLabel.setText("MVC框架");
		
		final Combo mvcCombo = new Combo(techListGroup, SWT.SINGLE | SWT.READ_ONLY);
		mvcCombo.add("SpringMVC");
		mvcCombo.add("Struts2MVC");
		mvcCombo.select(0);
		mvcCombo.addSelectionListener(new SelectionAdapter(){
			@Override
			public void widgetSelected(SelectionEvent event) {
				project.setMvcProtocol(mvcCombo.getText());
			}
		});
	}
	
	private void generateLocationOption(Composite parent) {
		final Composite composite = new Composite(parent, SWT.NONE);

		GridData modulesGridData = new GridData(GridData.FILL_HORIZONTAL);
		modulesGridData.grabExcessHorizontalSpace = true;
		
		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 2;
		
		composite.setLayout(gridLayout);
		composite.setLayoutData(modulesGridData);
		
		final Text locationText = new Text(composite, SWT.BORDER);
		locationText.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, true, false));
		
		String path = project.getPath();
		if (System.getProperty("os.name").toLowerCase().contains("windows")) {
			path = path.substring(1, path.length()).replaceAll("/", "\\\\");
		}
		locationText.setText(path);
		
		final String oldPath = path;
		Button locationButton = new Button(composite, SWT.NONE);
		locationButton.setText("选择项目路径");
		locationButton.addSelectionListener(new SelectionListener() {
			
			@Override
			public void widgetSelected(SelectionEvent e) {
				DirectoryDialog directoryDialog = new DirectoryDialog(composite.getShell());
				directoryDialog.setFilterPath(oldPath);
				directoryDialog.open();
				
				project.setPath(directoryDialog.getFilterPath());
				locationText.setText(directoryDialog.getFilterPath());
			}
			
			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
			}
		});
		
	}
	
}
